package com.kaka.bus.view.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Display;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

import com.kaka.bus.AppCenter;
import com.kaka.bus.R;
import com.kaka.bus.util.Utils;

/**
 * Created by OnionMac on 2017/12/28.
 */

public abstract class BaseDialog extends Dialog {

    private Activity mActivity;
    protected Window mWindow;

    public BaseDialog(@NonNull Context context) {
        this(context, R.style.capitaldialog);
    }

    public BaseDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        mActivity = (Activity) context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        setCanceledOnTouchOutside(false);

        WindowManager windowManager = mActivity.getWindowManager();
        Display display = windowManager.getDefaultDisplay();

        mWindow = getWindow();
        if(getWindowAnim() != 0){
            mWindow.setWindowAnimations(getWindowAnim());
        }

        final WindowManager.LayoutParams lp= mWindow.getAttributes();
        //设置居中
        mWindow.setGravity(Gravity.CENTER);
        lp.y = Utils.dp2px(AppCenter.getInstance(),moveY());
        lp.alpha=1.0f;
        lp.width = (int) (display.getWidth() * getWidthPer());
        setCanceledOnTouchOutside(false);
        getWindow().setAttributes(lp);
        initView();
    }

    protected int moveY() {
        return 0;
    }

    protected void setWindowBg(int res){
        mWindow.setBackgroundDrawableResource(res);
    }

    protected double getWidthPer(){
        return 0.65;
    }

    protected abstract int getLayoutId();

    protected abstract void initView();

    protected int getWindowAnim(){
        return 0;
    }
}
